package com.egu.web.controller;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.egu.common.util.EmailUtil;
import com.egu.model.Customer;
import com.egu.model.EmailInfo;
import com.egu.service.CustomerService;
import com.egu.service.EmailInfoService;
import com.egu.vo.EmailInfoVo;
import com.egu.vo.PageParam;
import com.egu.vo.RequestPageVo;
import com.egu.vo.RequestResultVo;

@Controller
@RequestMapping("emailInfo")
public class EmailInfoController {

	@Autowired
	CustomerService customerService;

	@Autowired
	private EmailInfoService emailInfoService;

	@RequestMapping("outboxListUI")
	public String outboxListUI() {
		return "emailInfo/outboxListUI";
	}

	@RequestMapping("draftboxListUI")
	public String draftboxListUI() {
		return "emailInfo/draftboxListUI";
	}

	@RequestMapping("sendEmailUI")
	public String sendEmailUI(@RequestParam(value = "id", required = false) Integer id, Model model) {
		if (id != null) {
			EmailInfoVo emailInfoVo = emailInfoService.getDataById(id);
			model.addAttribute("emailInfoVo", emailInfoVo);
		} else {
			model.addAttribute("emailInfoVo", null);
		}
		return "emailInfo/sendEmailUI";
	}

	// rder = "asc" offset = 0 limit = 10;
	@RequestMapping("/getOutboxList")
	@ResponseBody
	public RequestPageVo<EmailInfoVo> getOutboxList(@ModelAttribute(value = "param") PageParam param,
			EmailInfoVo emailInfoVo) {
		emailInfoVo.setEmailState("1");
		RequestPageVo<EmailInfoVo> vo = emailInfoService.getVoList(param, emailInfoVo);
		return vo;
	}

	@RequestMapping("/getDraftboxList")
	@ResponseBody
	public RequestPageVo<EmailInfoVo> getDraftboxList(@ModelAttribute(value = "param") PageParam param,
			EmailInfoVo emailInfoVo) {
		emailInfoVo.setEmailState("0");
		RequestPageVo<EmailInfoVo> vo = emailInfoService.getVoList(param, emailInfoVo);
		return vo;
	}

	@RequestMapping("/saveEmail")
	public String saveOrUpdate(EmailInfo emailInfo) {
		emailInfoService.saveOrUpdate(emailInfo);
		return "redirect:draftboxListUI";
	}

	@RequestMapping("/sendEmail")
	public String sendEmail(EmailInfo emailInfo) {
		Customer customer = customerService.getCustomertById(emailInfo.getCustomerId());
		/*if (customer == null) {
			result.setErrorMessage("客户不存，发送邮件失败");
			return result;
		}*/
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("emailInfo", emailInfo);
		map.put("customer", customer);
		EmailUtil.sendEmail(map);
		emailInfoService.saveOrUpdate(emailInfo);
		return "redirect:outboxListUI";
	}

	@RequestMapping("/deleteDataById")
	public @ResponseBody RequestResultVo<EmailInfo> deleteDataById(
			@RequestParam(value = "id", required = false) Integer id) {
		RequestResultVo<EmailInfo> result = new RequestResultVo<EmailInfo>();
		if (id == null) {
			result.setSuccess(false);
			result.setErrorMessage("传入id为空");
			return result;
		}
		result = emailInfoService.deleteDataById(id);
		return result;
	}
}
